package com.hmdp.config;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.hmdp.utils.LoginIntercepter;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@Configuration
public class MvcConfig implements WebMvcConfigurer {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginIntercepter(stringRedisTemplate))
                .excludePathPatterns(
                        "/user/code",
                        "/user/login",
                        "/blog/hot",
                        "/shop/**",
                        "/shop-type/**",
                        "/upload/**",
                        "/voucher/**",
                        "/doc.html",  // 添加这一行
                        "/webjars/**",  // 添加这一行
                        "/swagger-ui.html",
                        "/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**",
                        "/api", "/api-docs", "/api-docs/**",
                        "/doc.html/**"
                );
    }
//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        /**
//         * boot1可以选择不配置，则默认全部可以访问;boot2 必须配置
//         */
//        // 解决静态资源无法访问
//        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
//        // 解决swagger无法访问
//        registry.addResourceHandler("/webjars/**")
//                .addResourceLocations("classpath:/META-INF/resources/webjars/");
//        registry.addResourceHandler("/swagger-ui.html")
//                .addResourceLocations("classpath:/META-INF/resources/");
//        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
//
//    }

    /**
     * 注册自定义类型转换器
     */
//    @Override
//    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//        // 获取Json转换器
//        MappingJackson2HttpMessageConverter converter = (MappingJackson2HttpMessageConverter) converters.get(0);
//        // 转换Long和long类型
//        SimpleModule module = new SimpleModule();
//        module.addSerializer(Long.class, new LongToStringSerializer());
//        module.addSerializer(long.class, new LongToStringSerializer());
//        // 将自定义序列化器注册进Json转换器中
//        converter.getObjectMapper().registerModule(module);
//    }
//    /**
//     * 自定义LongToString序列化器
//     */
//    public static class LongToStringSerializer extends JsonSerializer<Long> {
//        @Override
//        public void serialize(Long value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
//            if (null != value) {
//                gen.writeString(value.toString());
//            } else {
//                gen.writeNull();
//            }
//        }
//    }

}
